{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "34fa8f1a9fbf4d15933d922bf180ffc4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], background_color=16777215, camera=[0.034641016151377546, 0.03464101615…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import trimesh\n",
    "import networkx as nx\n",
    "\n",
    "# test on a sphere mesh\n",
    "mesh = trimesh.primitives.Sphere()\n",
    "\n",
    "# edges without duplication\n",
    "edges = mesh.edges_unique\n",
    "\n",
    "# the actual length of each unique edge\n",
    "length = mesh.edges_unique_length\n",
    "\n",
    "# create the graph with edge attributes for length\n",
    "g = nx.Graph()\n",
    "for edge, L in zip(edges, length):\n",
    "    g.add_edge(*edge, length=L)\n",
    "\n",
    "# alternative method for weighted graph creation\n",
    "# you can also create the graph with from_edgelist and\n",
    "# a list comprehension, which is like 1.5x faster\n",
    "ga = nx.from_edgelist([(e[0], e[1], {\"length\": L}) for e, L in zip(edges, length)])\n",
    "\n",
    "# arbitrary indices of mesh.vertices to test with\n",
    "start, end = 0, int(len(mesh.vertices) / 2.0)\n",
    "\n",
    "# run the shortest path query using length for edge weight\n",
    "path = nx.shortest_path(g, source=start, target=end, weight=\"length\")\n",
    "\n",
    "# VISUALIZE RESULT\n",
    "# make the sphere transparent-ish\n",
    "mesh.visual.face_colors = [100, 100, 100, 100]\n",
    "\n",
    "# Path3D with the path between the points\n",
    "path_visual = trimesh.load_path(mesh.vertices[path])\n",
    "\n",
    "# visualizable two points\n",
    "points_visual = trimesh.points.PointCloud(mesh.vertices[[start, end]])\n",
    "\n",
    "\n",
    "from vtkplotter import show\n",
    "\n",
    "show(mesh, points_visual, path_visual)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
